﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Traceability.Webapi.Controllers
{
    /// <summary>
    /// 上传 jpg 文件
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FliesController : ControllerBase
    {
        /// <summary>
        /// 图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]

        public IActionResult Upimage(IFormFile file)
        {
            //获取文件名
            var fileName = file.FileName;
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(fileName);
            //获取文件存放的路径
            var path = Directory.GetCurrentDirectory() + @"\\wwwroot\\Images\\" + fileName;
            //创建文件流
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);

                fs.Flush();
            }

            //返回前端
            var url = "/Images/" + fileName;
            return Ok(url);
        }
    }
}
